﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Quellenverwaltung.Data.Source;
using Quellenverwaltung.Word;

namespace Quellenverwaltung
{
	/// <summary>
	/// Interaktionslogik für EditJournal.xaml
	/// </summary>
	public partial class EditJournal : UserControl
	{
        public Quellenverwaltung.UI.Forms.JournalHost HostForm { get; set; }
        private ArticleInAPeriodical Journal { get; set; }
        private Brush borderBrush;
        private Brush backgroundBrush;

        public EditJournal()
		{
			this.InitializeComponent();

            borderBrush = textTitle.BorderBrush;
            backgroundBrush = textTitle.Background;

            textAuthor.BorderBrush = Brushes.Transparent;
            textUrl.BorderBrush = Brushes.Transparent;
            textYear.BorderBrush = Brushes.Transparent;
            textMonth.BorderBrush = Brushes.Transparent;
            textDay.BorderBrush = Brushes.Transparent;
            textYearAccessed.BorderBrush = Brushes.Transparent;
            textMonthAccessed.BorderBrush = Brushes.Transparent;
            textDayAccessed.BorderBrush = Brushes.Transparent;
            textStandardNumber.BorderBrush = Brushes.Transparent;
            textVolume.BorderBrush = Brushes.Transparent;
            textShortTitle.BorderBrush = Brushes.Transparent;
            textPublisher.BorderBrush = Brushes.Transparent;
            textPeriodicalTitle.BorderBrush = Brushes.Transparent;
            textPages.BorderBrush = Brushes.Transparent;
            textMedium.BorderBrush = Brushes.Transparent;
            textIssue.BorderBrush = Brushes.Transparent;
            textEdition.BorderBrush = Brushes.Transparent;
            textDOI.BorderBrush = Brushes.Transparent;

            textTitle.Background = Brushes.Transparent;
            textAuthor.Background = Brushes.Transparent;
            textUrl.Background = Brushes.Transparent;
            textYear.Background = Brushes.Transparent;
            textMonth.Background = Brushes.Transparent;
            textDay.Background = Brushes.Transparent;
            textYearAccessed.Background = Brushes.Transparent;
            textMonthAccessed.Background = Brushes.Transparent;
            textDayAccessed.Background = Brushes.Transparent;
            textStandardNumber.Background = Brushes.Transparent;
            textVolume.Background = Brushes.Transparent;
            textShortTitle.Background = Brushes.Transparent;
            textPublisher.Background = Brushes.Transparent;
            textPeriodicalTitle.Background = Brushes.Transparent;
            textPages.Background = Brushes.Transparent;
            textMedium.Background = Brushes.Transparent;
            textIssue.Background = Brushes.Transparent;
            textEdition.Background = Brushes.Transparent;
            textDOI.Background = Brushes.Transparent;

            // Leeres Buch anlegen
            this.Journal = new ArticleInAPeriodical();
            Journal.Author = new Authors();
            Journal.Author.Author = new Author();
            Journal.Author.Translator = new Author();
            Journal.Author.Editor = new Author();
            setJournalDataSource(Journal);
		}

        private void input_MouseEnter(object sender, MouseEventArgs e)
        {
            ((TextBox)sender).BorderBrush = borderBrush;
            ((TextBox)sender).Background = backgroundBrush;
        }

        private void input_MouseLeave(object sender, MouseEventArgs e)
        {
            ((TextBox)sender).BorderBrush = Brushes.Transparent;
            ((TextBox)sender).Background = Brushes.Transparent;
        }

        public void setJournalDataSource(ArticleInAPeriodical journal)
        {
            DataContext = null;
            this.Journal = journal;
            DataContext = Journal;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            handleAuthorInput();

            CitationHelper helper = new CitationHelper();
            helper.addJournal(this.Journal);
            HostForm.Close();
        }

        private void ButtonAddSourrceAndCite_Click(object sender, RoutedEventArgs e)
        {
            handleAuthorInput();

            CitationHelper helper = new CitationHelper();
            String tag = helper.addJournal(this.Journal);
            helper.addCitation(tag);
            HostForm.Close();
        }

        private void handleAuthorInput()
        {
            if (AuthorIsCorporateCheckBox.IsChecked != null && ((bool)AuthorIsCorporateCheckBox.IsChecked))
            {
                this.Journal.Author.Author.NameList = null;
            }
            else
            {
                this.Journal.Author.Author.Corporate = null;
            }
        }

        private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
        {
            useCompanyAsAuthor();
        }

        private void CheckBox_Unchecked_1(object sender, RoutedEventArgs e)
        {
            useNameAsAuthor();
        }

        private void useCompanyAsAuthor()
        {
            labelAuthor.Content = "Author (Firma)";
            adornerAuthor.Visibility = Visibility.Hidden;
            adornerAuthorCompany.Visibility = Visibility.Visible;
        }

        private void useNameAsAuthor()
        {
            labelAuthor.Content = "Author";
            adornerAuthor.Visibility = Visibility.Visible;
            adornerAuthorCompany.Visibility = Visibility.Hidden;
        }
	}
}